﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:DlyndxOrder
    /// </summary>
    public partial class DlyndxOrder
    {
        public DlyndxOrder()
        {
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal Vchcode)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from DlyndxOrder");
            strSql.Append(" where Vchcode=@Vchcode");
            var parameters = new SqlParameter[] { new SqlParameter("@Vchcode", SqlDbType.Decimal)
            };
            parameters[0].Value = Vchcode;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public decimal Add(DMZS.Model.DlyndxOrder model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into DlyndxOrder(");
            strSql.Append("Date,Number,VchType,Comment,btypeid,etypeid,ktypeid,ktypeid2,ifcheck,checke,Period,InputNo,draft,deptid,IsStop,todate,defdiscount,checkeDate)");
            strSql.Append(" values (");
            strSql.Append("@Date,@Number,@VchType,@Comment,@btypeid,@etypeid,@ktypeid,@ktypeid2,@ifcheck,@checke,@Period,@InputNo,@draft,@deptid,@IsStop,@todate,@defdiscount,@checkeDate)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@Date", SqlDbType.DateTime),
                    new SqlParameter("@Number", SqlDbType.VarChar, 40),
                    new SqlParameter("@VchType", SqlDbType.Decimal, 5),
                    new SqlParameter("@Comment", SqlDbType.VarChar, 256),
                    new SqlParameter("@btypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@etypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@ktypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@ktypeid2", SqlDbType.VarChar, 25),
                    new SqlParameter("@ifcheck", SqlDbType.Char, 1),
                    new SqlParameter("@checke", SqlDbType.VarChar, 25),
                    new SqlParameter("@Period", SqlDbType.SmallInt, 2),
                    new SqlParameter("@InputNo", SqlDbType.VarChar, 25),
                    new SqlParameter("@draft", SqlDbType.Int, 4),
                    new SqlParameter("@deptid", SqlDbType.VarChar, 25),
                    new SqlParameter("@IsStop", SqlDbType.Int, 4),
                    new SqlParameter("@todate", SqlDbType.DateTime),
                    new SqlParameter("@defdiscount", SqlDbType.Decimal, 9),
                    new SqlParameter("@checkeDate", SqlDbType.DateTime) };
            parameters[0].Value = model.Date;
            parameters[1].Value = model.Number;
            parameters[2].Value = model.VchType;
            parameters[3].Value = model.Comment;
            parameters[4].Value = model.btypeid;
            parameters[5].Value = model.etypeid;
            parameters[6].Value = model.ktypeid;
            parameters[7].Value = model.ktypeid2;
            parameters[8].Value = model.ifcheck;
            parameters[9].Value = model.checke;
            parameters[10].Value = model.Period;
            parameters[11].Value = model.InputNo;
            parameters[12].Value = model.draft;
            parameters[13].Value = model.deptid;
            parameters[14].Value = model.IsStop;
            parameters[15].Value = model.todate;
            parameters[16].Value = model.defdiscount;
            parameters[17].Value = model.checkeDate;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.DlyndxOrder model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update DlyndxOrder set ");
            strSql.Append("Date=@Date,");
            strSql.Append("Number=@Number,");
            strSql.Append("VchType=@VchType,");
            strSql.Append("Comment=@Comment,");
            strSql.Append("btypeid=@btypeid,");
            strSql.Append("etypeid=@etypeid,");
            strSql.Append("ktypeid=@ktypeid,");
            strSql.Append("ktypeid2=@ktypeid2,");
            strSql.Append("ifcheck=@ifcheck,");
            strSql.Append("checke=@checke,");
            strSql.Append("Period=@Period,");
            strSql.Append("InputNo=@InputNo,");
            strSql.Append("draft=@draft,");
            strSql.Append("deptid=@deptid,");
            strSql.Append("IsStop=@IsStop,");
            strSql.Append("todate=@todate,");
            strSql.Append("defdiscount=@defdiscount,");
            strSql.Append("checkeDate=@checkeDate");
            strSql.Append(" where Vchcode=@Vchcode");
            var parameters = new SqlParameter[] { new SqlParameter("@Date", SqlDbType.DateTime),
                    new SqlParameter("@Number", SqlDbType.VarChar, 40),
                    new SqlParameter("@VchType", SqlDbType.Decimal, 5),
                    new SqlParameter("@Comment", SqlDbType.VarChar, 256),
                    new SqlParameter("@btypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@etypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@ktypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@ktypeid2", SqlDbType.VarChar, 25),
                    new SqlParameter("@ifcheck", SqlDbType.Char, 1),
                    new SqlParameter("@checke", SqlDbType.VarChar, 25),
                    new SqlParameter("@Period", SqlDbType.SmallInt, 2),
                    new SqlParameter("@InputNo", SqlDbType.VarChar, 25),
                    new SqlParameter("@draft", SqlDbType.Int, 4),
                    new SqlParameter("@deptid", SqlDbType.VarChar, 25),
                    new SqlParameter("@IsStop", SqlDbType.Int, 4),
                    new SqlParameter("@todate", SqlDbType.DateTime),
                    new SqlParameter("@defdiscount", SqlDbType.Decimal, 9),
                    new SqlParameter("@checkeDate", SqlDbType.DateTime),
                    new SqlParameter("@Vchcode", SqlDbType.Decimal, 5) };
            parameters[0].Value = model.Date;
            parameters[1].Value = model.Number;
            parameters[2].Value = model.VchType;
            parameters[3].Value = model.Comment;
            parameters[4].Value = model.btypeid;
            parameters[5].Value = model.etypeid;
            parameters[6].Value = model.ktypeid;
            parameters[7].Value = model.ktypeid2;
            parameters[8].Value = model.ifcheck;
            parameters[9].Value = model.checke;
            parameters[10].Value = model.Period;
            parameters[11].Value = model.InputNo;
            parameters[12].Value = model.draft;
            parameters[13].Value = model.deptid;
            parameters[14].Value = model.IsStop;
            parameters[15].Value = model.todate;
            parameters[16].Value = model.defdiscount;
            parameters[17].Value = model.checkeDate;
            parameters[18].Value = model.Vchcode;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal Vchcode)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from DlyndxOrder ");
            strSql.Append(" where Vchcode=@Vchcode");
            var parameters = new SqlParameter[] { new SqlParameter("@Vchcode", SqlDbType.Decimal)
            };
            parameters[0].Value = Vchcode;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Vchcodelist)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from DlyndxOrder ");
            strSql.Append(" where Vchcode in (" + Vchcodelist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.DlyndxOrder GetModel(decimal Vchcode)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 Vchcode,Date,Number,VchType,Comment,btypeid,etypeid,ktypeid,ktypeid2,ifcheck,checke,Period,InputNo,draft,deptid,IsStop,todate,defdiscount,checkeDate from DlyndxOrder ");
            strSql.Append(" where Vchcode=@Vchcode");
            var parameters = new SqlParameter[] { new SqlParameter("@Vchcode", SqlDbType.Decimal)
            };
            parameters[0].Value = Vchcode;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.DlyndxOrder DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.DlyndxOrder();
            if (row != null)
            {
                if (row["Vchcode"] != null && row["Vchcode"].ToString() != string.Empty)
                {
                    model.Vchcode = decimal.Parse(row["Vchcode"].ToString());
                }
                if (row["Date"] != null && row["Date"].ToString() != string.Empty)
                {
                    model.Date = DateTime.Parse(row["Date"].ToString());
                }
                if (row["Number"] != null)
                {
                    model.Number = row["Number"].ToString();
                }
                if (row["VchType"] != null && row["VchType"].ToString() != string.Empty)
                {
                    model.VchType = decimal.Parse(row["VchType"].ToString());
                }
                if (row["Comment"] != null)
                {
                    model.Comment = row["Comment"].ToString();
                }
                if (row["btypeid"] != null)
                {
                    model.btypeid = row["btypeid"].ToString();
                }
                if (row["etypeid"] != null)
                {
                    model.etypeid = row["etypeid"].ToString();
                }
                if (row["ktypeid"] != null)
                {
                    model.ktypeid = row["ktypeid"].ToString();
                }
                if (row["ktypeid2"] != null)
                {
                    model.ktypeid2 = row["ktypeid2"].ToString();
                }
                if (row["ifcheck"] != null)
                {
                    model.ifcheck = row["ifcheck"].ToString();
                }
                if (row["checke"] != null)
                {
                    model.checke = row["checke"].ToString();
                }
                if (row["Period"] != null && row["Period"].ToString() != string.Empty)
                {
                    model.Period = int.Parse(row["Period"].ToString());
                }
                if (row["InputNo"] != null)
                {
                    model.InputNo = row["InputNo"].ToString();
                }
                if (row["draft"] != null && row["draft"].ToString() != string.Empty)
                {
                    model.draft = int.Parse(row["draft"].ToString());
                }
                if (row["deptid"] != null)
                {
                    model.deptid = row["deptid"].ToString();
                }
                if (row["IsStop"] != null && row["IsStop"].ToString() != string.Empty)
                {
                    model.IsStop = int.Parse(row["IsStop"].ToString());
                }
                if (row["todate"] != null && row["todate"].ToString() != string.Empty)
                {
                    model.todate = DateTime.Parse(row["todate"].ToString());
                }
                if (row["defdiscount"] != null && row["defdiscount"].ToString() != string.Empty)
                {
                    model.defdiscount = decimal.Parse(row["defdiscount"].ToString());
                }
                if (row["checkeDate"] != null && row["checkeDate"].ToString() != string.Empty)
                {
                    model.checkeDate = DateTime.Parse(row["checkeDate"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select Vchcode,Date,Number,VchType,Comment,btypeid,etypeid,ktypeid,ktypeid2,ifcheck,checke,Period,InputNo,draft,deptid,IsStop,todate,defdiscount,checkeDate ");
            strSql.Append(" FROM DlyndxOrder ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Vchcode,Date,Number,VchType,Comment,btypeid,etypeid,ktypeid,ktypeid2,ifcheck,checke,Period,InputNo,draft,deptid,IsStop,todate,defdiscount,checkeDate ");
            strSql.Append(" FROM DlyndxOrder ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM DlyndxOrder ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Vchcode desc");
            }
            strSql.Append(")AS Row, T.*  from DlyndxOrder T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }


        


        /// <summary>
        /// 获取头部
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public virtual DataRow GetHead(long ID)
        {
            var strSql = @"
select 
	h.Vchcode,h.Date,h.Number,h.VchType,h.Comment,
	h.btypeid,h.etypeid,h.ktypeid,h.ktypeid2,h.ifcheck,
	h.checke,h.Period,h.InputNo,h.draft,h.deptid,
	h.IsStop,h.todate,h.defdiscount,h.checkeDate,h.creator,
	s.Code SupplierClientCode,s.Name SupplierClientName,
	w.Code WarehouseCode,w.Name WarehouseName,
    d.Code DepartmentCode,d.Name DepartmentName
from DlyndxOrder h
join Vchtype v on v.vchtype=h.VchType
left join DepartmentNew d on d.ID=h.deptid
left join Supplier s on s.ID=h.btypeid
left join Warehouse w on w.ID=h.ktypeid
where h.Vchcode=@ID --and v.vCode='CGDD'
";
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.BigInt, 8)
            };
            parameters[0].Value = ID;

            var ds = DbHelperSQL.Query(strSql, parameters);

            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }


        /// <summary>
        /// 获取表体
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public virtual DataTable GetBody(long ID)
        {
            var strSql = @"
select 
	h.Vchcode,h.Date,h.Number,h.VchType,h.Comment,
	h.btypeid,h.etypeid,h.ktypeid,h.ktypeid2,h.ifcheck,
	h.checke,h.Period,h.InputNo,h.draft,h.deptid,
	h.IsStop,h.todate,h.defdiscount,h.checkeDate,h.creator,
	b.DlyOrder,b.PtypeId,b.Qty,b.unit,b.Price,(b.Qty*b.Price) SubTotal,
	b.unit1,b.qty1,b.comment BodyComment,b.qtytotal,b.Date BodyDate,
	s.Code SupplierClientCode,s.Name SupplierClientName,
	w.Code WarehouseCode,w.Name WarehouseName,
	u.BaseCode UnitBaseCode, u.BaseName UnitBaseName,
    p.Code ProductCode,p.Name ProductName
from DlyndxOrder h
join DlySale b on b.Vchcode=h.Vchcode
join Vchtype v on v.vchtype=h.VchType
left join Product p on p.ID=b.PtypeId
left join Supplier s on s.ID=h.btypeid
left join Warehouse w on w.ID=h.ktypeid
left join UnitBase u on u.UnitBaseID=b.unit1
where h.Vchcode=@ID --and v.vCode='CGDD'
";
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.BigInt, 8)
            };
            parameters[0].Value = ID;

            var ds = DbHelperSQL.Query(strSql, parameters);

            if (ds == null)
            {
                return null;
            }
            return ds.Tables[0];
        }

        

        /// <summary>
        /// 审核
        /// -1000:被删除
        /// -10001:已审核
        /// -1003:已关闭
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="checke"></param>
        /// <returns></returns>
        public virtual int Check(long ID, string checke)
        {
            var strSql = @"
declare @draft int
select @draft=draft from DlyndxOrder where Vchcode=@Vchcode

if @draft is null begin
    select -1000
    return
end

if @draft=1 begin
    select -10001
    return
end

if @draft=2 begin
    select -1003
    return
end

update DlyndxOrder set ifcheck='1', checke=@checke,draft=1 where Vchcode=@Vchcode
select @Vchcode
";
            var parameters = new SqlParameter[] { new SqlParameter("@checke", SqlDbType.VarChar, 25),
                    new SqlParameter("@Vchcode", SqlDbType.Decimal) };
            parameters[0].Value = checke;
            parameters[1].Value = ID;

            var obj = DbHelperSQL.GetSingle(strSql, parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 反审核
        /// -1000:被删除
        /// -10002:未审核
        /// -1003:已关闭
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public virtual int UnCheck(long ID)
        {
            var strSql = @"
declare @draft int
select @draft=draft from DlyndxOrder where Vchcode=@Vchcode

if @draft is null begin
    select -1000
    return
end

if @draft=0 begin
    select -10002
    return
end

if @draft=2 begin
    select -1003
    return
end

update DlyndxOrder set ifcheck='0', checke=@checke,draft=0 where Vchcode=@Vchcode
select @Vchcode
";
            var parameters = new SqlParameter[] { new SqlParameter("@checke", SqlDbType.VarChar, 25),
                    new SqlParameter("@Vchcode", SqlDbType.Decimal) };
            parameters[0].Value = null;
            parameters[1].Value = ID;

            var obj = DbHelperSQL.GetSingle(strSql, parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 关闭
        /// -1000:被删除
        /// -10002:未审核
        /// -1003:已关闭
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public virtual int Close(long ID)
        {
            var strSql = @"
declare @draft int
select @draft=draft from DlyndxOrder where Vchcode=@Vchcode

if @draft is null begin
    select -1000
    return
end

if @draft=0 begin
    select -10002
    return
end

if @draft=2 begin
    select -1003
    return
end

update DlyndxOrder set draft=2,IsStop=1 where Vchcode=@Vchcode
select @Vchcode
";
            var parameters = new SqlParameter[] { new SqlParameter("@Vchcode", SqlDbType.Decimal) };
            parameters[0].Value = ID;

            var obj = DbHelperSQL.GetSingle(strSql, parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public virtual int DeleteExpand(long ID)
        {
            var strSql = @"
begin tran

declare @draft int
select @draft=draft from DlyndxOrder where Vchcode=@Vchcode
if @draft is null begin
    commit tran
    select 1    --已被删
    return
end

if @draft=1 begin
    commit tran
    select -10031   --已审核
    return
end

if @draft=2 begin
    commit tran
    select -10032   --已关闭
    return
end

delete DlyndxOrder where Vchcode=@Vchcode
delete DlySale where Vchcode=@Vchcode

if @@error<>0 begin
    rollback tran
    select 0
end else begin
    commit tran
    select 1
end
";
            var parameters = new SqlParameter[] { new SqlParameter("@Vchcode", SqlDbType.BigInt, 8) };
            parameters[0].Value = ID;
            var obj = DbHelperSQL.GetSingle(strSql, parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
    }
}

